package com.bw.module_detail.widget
import android.app.Activity
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.text.TextPaint
import com.bw.module_detail.R
import master.flame.danmaku.danmaku.model.BaseDanmaku
import master.flame.danmaku.danmaku.model.android.AndroidDisplayer.DisplayerConfig
import master.flame.danmaku.danmaku.model.android.BaseCacheStuffer
/**
 * @author ykj
 * @date 2025-10-24 20:44
 * @description
 */
class MyCacheStuffer(activity: Activity) : BaseCacheStuffer() {
    /**
     * 文字右边间距
     */
    private val RIGHTMARGE = activity.resources.getDimension(R.dimen.dp_22)

    /**
     * 文字和头像间距
     */
    // 初始化固定参数，这些参数可以根据自己需求自行设定
    private val LEFTMARGE = activity.resources.getDimension(R.dimen.dp_13)

    /**
     * 文字和右边线距离
     */
    private val TEXT_RIGHT_PADDING = 0

    /**
     * 文字大小
     */
    private val TEXT_SIZE = activity.resources.getDimension(R.dimen.sp_16)

    /**
     * 头像的大小
     */
    private val IMAGEHEIGHT = activity.resources.getDimension(R.dimen.dp_24)

    private  val paint = Paint()

    override fun measure(danmaku: BaseDanmaku, paint: TextPaint, fromWorkerThread: Boolean) {
        // 初始化数据
        val map = danmaku.tag as Map<String, Any>
        val content = map["content"] as String?

        // 设置画笔
        paint.textSize = TEXT_SIZE

        // 计算名字和内容的长度，取最大值
        val contentWidth = paint.measureText(content)

        // 设置弹幕区域的宽度
        danmaku.paintWidth = contentWidth + IMAGEHEIGHT + LEFTMARGE + RIGHTMARGE
        // 设置弹幕区域的高度
        danmaku.paintHeight = IMAGEHEIGHT * 1.2f
    }

    override fun clearCaches() {
    }

    override fun drawDanmaku(
        danmaku: BaseDanmaku,
        canvas: Canvas,
        left: Float,
        top: Float,
        fromWorkerThread: Boolean,
        displayerConfig: DisplayerConfig?
    ) {
        // 初始化数据
        val map = danmaku.tag as Map<String, Any>
        val content = map["content"] as String?
        val bitmap = map["bitmap"] as Bitmap?
        val color = map["color"] as String?

        // 设置画笔
        paint.textSize = TEXT_SIZE
        paint.color = Color.parseColor(color)

        //文字宽度
        val textLength = paint.measureText(content).toInt()
        //获取图片的宽度
        val rectBgLeft = left
        val rectBgTop = top
        val rectBgRight = left + IMAGEHEIGHT + textLength + LEFTMARGE + RIGHTMARGE
        val rectBgBottom = top + IMAGEHEIGHT
        //绘制背景
        canvas.drawRoundRect(
            RectF(rectBgLeft, rectBgTop, rectBgRight, rectBgBottom),
            IMAGEHEIGHT / 2,
            IMAGEHEIGHT / 2,
            paint
        )

        // 绘制头像
        val avatorRight = left + IMAGEHEIGHT
        val avatorBottom = top + IMAGEHEIGHT
        canvas.drawBitmap(bitmap!!, null, RectF(left, top, avatorRight, avatorBottom), paint)

        // 绘制弹幕内容,文字白色的
        paint.color = Color.WHITE
        val contentLeft = left + IMAGEHEIGHT + LEFTMARGE
        //计算文字的相应偏移量
        val fontMetrics = paint.fontMetrics
        //为基线到字体上边框的距离,即上图中的top
        val textTop = fontMetrics.top
        //为基线到字体下边框的距离,即上图中的bottom
        val textBottom = fontMetrics.bottom

        val contentBottom = top + IMAGEHEIGHT / 2
        //基线中间点的y轴计算公式
        val baseLineY = (contentBottom - textTop / 2 - textBottom / 2).toInt()
        //绘制文字
        canvas.drawText(content!!, contentLeft, baseLineY.toFloat(), paint)
    }
}